package com.folderit.apps.rooms;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.folderit.apps.rooms.sync.RoomsSyncAdapter;

public class MainActivity extends ActionBarActivity
        implements RoomsFragment.RoomSelectedCallback,
        ReservationsFragment.ReservationSelectedCallback {

    private boolean mThreePane;
    private Fragment mDetailFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (findViewById(R.id.reservations_container) != null) {

            // The detail container view will be present only in the large-screen layouts
            // (res/layout-sw600dp). If this view is present, then the activity should be
            // in two-pane mode.
            mThreePane = true;

            // In two-pane mode, show the detail view in this activity by
            // adding or replacing the detail fragment using a
            // fragment transaction.
            if (savedInstanceState == null) {
                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.reservations_container, new ReservationsFragment())
                        .commit();
            }
        } else {
            mThreePane = false;
        }

        RoomsFragment roomsFragment =  ((RoomsFragment)getSupportFragmentManager()
                .findFragmentById(R.id.fragment_rooms));
        // roomsFragment.setWhatever(true)

        RoomsSyncAdapter.initializeSyncAdapter(this);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            startActivity(new Intent(this, SettingsActivity.class));
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onRoomSelected(int roomId) {
        if (mThreePane) {
            // In three-pane mode, show the reservation list in this activity by
            // adding or replacing the reservation fragment using a fragment transaction.
            Bundle args = new Bundle();
            args.putInt(ReservationsActivity.SELECTED_ROOM_KEY, roomId);

            ReservationsFragment fragment = new ReservationsFragment();
            fragment.setArguments(args);

            // Replace the list of reservations
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.reservations_container, fragment)
                    .commit();

            // Delete the detail fragment (if exists)
            if (mDetailFragment != null) {
                getSupportFragmentManager().beginTransaction()
                        .remove(mDetailFragment)
                        .commit();
            }
        } else {
            Intent intent = new Intent(this, ReservationsActivity.class)
                    .putExtra(ReservationsActivity.SELECTED_ROOM_KEY, roomId);
            startActivity(intent);
        }
    }

    @Override
    public void onReservationSelected(int reservationId) {
        if (mThreePane) {
            // In three-pane mode, show the reservation detail view in this activity by
            // adding or replacing the detail fragment using a fragment transaction.
            Bundle args = new Bundle();
            args.putInt(ReservationDetailsActivity.SELECTED_RESERVATION_KEY, reservationId);

            ReservationDetailsFragment fragment = new ReservationDetailsFragment();
            fragment.setArguments(args);

            mDetailFragment = fragment;

            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.reservation_details_container, fragment)
                    .commit();
        }
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }
}
